/**
 *
 */
package de.cbf.cam.dsa.facet.item.editor;

import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_COMPOSITE_BROKENNESS;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_COMPOSITE_DIRTINESS;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_TEXT_DESCRIPTION_ID;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_TEXT_QUALITY_MAX_ID;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_TEXT_QUALITY_MIN_ID;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_TEXT_TYPE_ID;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_TEXT_VALUE_KREUZER_ID;
import static de.cbf.cam.dsa.facet.item.test_support.ConstantsSWTBOTItem.FACET_ITEM_TEXT_WEIGHT_ID;
import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import de.cbf.cam.dsa.facet.item.editor.shapeness.CompositeEditShapenessDescriptions;
import de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet;
import de.cbf.util.entity.facet.edit.util.FieldDecoratorUtil;
import de.cbf.util.ui.listener.UiChangeEvent;

/**
 * @author Nebelritter
 *
 */
public class CompositeEditItem extends AbstractCompositeEditFacet {

	public static final int IDX_TYPE			= 3;
	public static final int IDX_DESCRIPTION		= 4;
	public static final int IDX_WEIGHT			= 5;
	public static final int IDX_QUALITY_MAX		= 6;
	public static final int IDX_QUALITY_MIN		= 7;
	public static final int IDX_VALUE_KREUZER	= 8;

	protected static final String WEIGHT_TEXT		= "Weight:";
	protected static final String TYPE_TEXT			= "Type:";
	protected static final String QUALITY_MIN_TEXT	= "Min. quality:";
	protected static final String QUALITY_MAX_TEXT	= "Max. quality:";
	protected static final String VALUE_KREUZER		= "Value(K):";
	protected static final String DESCRIPTION_TEXT	= "Description:";
	protected static final String BROKENNESS_TEXT	= "Brokenness:";
	protected static final String DIRTINESS_TEXT	= "Dirtiness:";	

	protected Text tx_Weight;
	protected Text tx_Description;
	protected Text tx_Type;
	protected Text tx_ValueKreuzer;
	protected Text tx_QualityMax;
	protected Text tx_QualityMin;
	protected CompositeEditShapenessDescriptions cp_Brokenness;
	protected CompositeEditShapenessDescriptions cp_Dirtiness;
	
	protected ControlDecoration decoration_weight;
	protected ControlDecoration decoration_QualityMin;
	protected ControlDecoration decoration_QualityMax;
	protected ControlDecoration decoration_ValueKreuzer;	
	
	public CompositeEditItem(Composite parent, int style,FormToolkit toolkit) {
		super(parent, style,toolkit);
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet#createLayout()
	 */
	@Override
	protected GridLayout createLayout() {
		GridLayout result = new GridLayout(2,false);
		return result;
	}
	
	@Override
	protected void createContent(final Composite compositeThis){
		//weight
		Label lbWeight = new Label(compositeThis, SWT.NONE);
		lbWeight.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lbWeight.setText(WEIGHT_TEXT);

		tx_Weight = new Text(compositeThis, SWT.BORDER);
		tx_Weight.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		tx_Weight.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_TEXT_WEIGHT_ID);
		tx_Weight.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_Weight.getText());
				event.setIndex(IDX_WEIGHT);
				fireUIChangeEvent(event);
			}
		});		
		// Create the decoration for weight
		decoration_weight = new ControlDecoration(tx_Weight,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_weight = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_weight.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_weight.setImage(image_weight);
		decoration_weight.hide();

		//type
		Label lbType = new Label(compositeThis, SWT.NONE);
		lbType.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lbType.setText(TYPE_TEXT);

		tx_Type = new Text(compositeThis, SWT.BORDER);
		tx_Type.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		tx_Type.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_TEXT_TYPE_ID);
		tx_Type.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_Type.getText());
				event.setIndex(IDX_TYPE);
				fireUIChangeEvent(event);
			}
		});

		//min quality
		Label lbQualityMin = new Label(compositeThis, SWT.NONE);
		lbQualityMin.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lbQualityMin.setText(QUALITY_MIN_TEXT);
		tx_QualityMin = new Text(compositeThis, SWT.BORDER);
		tx_QualityMin.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		tx_QualityMin.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_TEXT_QUALITY_MIN_ID);
		tx_QualityMin.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_QualityMin.getText());
				event.setIndex(IDX_QUALITY_MIN);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for quality min
		decoration_QualityMin = new ControlDecoration(tx_QualityMin,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_QualityMin = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_QualityMin.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_QualityMin.setImage(image_QualityMin);
		decoration_QualityMin.hide();
		
		
		//max quality
		Label lbQualityMax = new Label(compositeThis, SWT.NONE);
		lbQualityMax.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lbQualityMax.setText(QUALITY_MAX_TEXT);
		tx_QualityMax = new Text(compositeThis, SWT.BORDER);
		tx_QualityMax.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		tx_QualityMax.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_TEXT_QUALITY_MAX_ID);
		tx_QualityMax.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_QualityMax.getText());
				event.setIndex(IDX_QUALITY_MAX);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for quality max
		decoration_QualityMax = new ControlDecoration(tx_QualityMax,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_QualityMax = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_QualityMax.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_QualityMax.setImage(image_QualityMax);
		decoration_QualityMax.hide();
				
		//value kreuzer
		Label lbValueKreuzer = new Label(compositeThis, SWT.NONE);
		lbValueKreuzer.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false,1,1));
		lbValueKreuzer.setText(VALUE_KREUZER);
		tx_ValueKreuzer = new Text(compositeThis, SWT.BORDER);
		tx_ValueKreuzer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,1,1));
		tx_ValueKreuzer.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_TEXT_VALUE_KREUZER_ID);
		tx_ValueKreuzer.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_ValueKreuzer.getText());
				event.setIndex(IDX_VALUE_KREUZER);
				fireUIChangeEvent(event);
			}
		});
		// Create the decoration for value kreuzer
		decoration_ValueKreuzer = new ControlDecoration(tx_ValueKreuzer,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_ValueKreuzer = FieldDecorationRegistry. getDefault(). getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_ValueKreuzer.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_INTEGER);
		decoration_ValueKreuzer.setImage(image_ValueKreuzer);
		decoration_ValueKreuzer.hide();
						
		//description
		Label lbDescription = new Label(compositeThis, SWT.NONE);
		lbDescription.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,2,1));
		lbDescription.setText(DESCRIPTION_TEXT);
		tx_Description = new Text(compositeThis, SWT.BORDER);
		GridData gd_Description = new GridData(SWT.FILL, SWT.FILL, true, true,2,2);
		gd_Description.heightHint = 40;
		tx_Description.setLayoutData(gd_Description);
		tx_Description.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_TEXT_DESCRIPTION_ID);
		tx_Description.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(compositeThis);
				event.setNewValue(tx_Description.getText());
				event.setIndex(IDX_DESCRIPTION);
				fireUIChangeEvent(event);
			}
		});

		Composite cp_shapenesses = new Composite(compositeThis, SWT.DOUBLE_BUFFERED);
		cp_shapenesses.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
		GridLayout glo_Shapenesses = new GridLayout(2,true);
		glo_Shapenesses.marginHeight = 0;
		glo_Shapenesses.marginWidth	 = 0;
		cp_shapenesses.setLayout(glo_Shapenesses);

		//brokenness
		cp_Brokenness = createShapenessComposite(BROKENNESS_TEXT,cp_shapenesses);
		cp_Brokenness.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,1,1));
		cp_Brokenness.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_COMPOSITE_BROKENNESS);
		//dirtiness
		cp_Dirtiness = createShapenessComposite(DIRTINESS_TEXT,cp_shapenesses);
		cp_Dirtiness.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,1,1));
		cp_Dirtiness.setData(DEFAULT_SWT_BOT_KEY, FACET_ITEM_COMPOSITE_DIRTINESS);
	}

	/**
	 * @param labelText
	 * @param compositeThis
	 */
	protected CompositeEditShapenessDescriptions createShapenessComposite(String labelText,Composite compositeThis) {
		CompositeEditShapenessDescriptions cp_Shapeness = new CompositeEditShapenessDescriptions(compositeThis, SWT.DOUBLE_BUFFERED);
		cp_Shapeness.setHeaderText(labelText);
		return cp_Shapeness;
	}

	public String getWeight(){
		return tx_Weight.getText();
	}

	public void setWeight(String newWeight) {
		if( newWeight != null && ! tx_Weight.getText().equals(newWeight)){
			Point selection = tx_Weight.getSelection();
			tx_Weight.setText(newWeight);
			tx_Weight.setSelection(selection);
		}
	}
	
	public String getDescription(){
		return tx_Description.getText();
	}

	public void setDescription(String newDescription) {
		if( newDescription != null && ! tx_Description.getText().equals(newDescription)){
			Point selection = tx_Description.getSelection();
			tx_Description.setText(newDescription);
			tx_Description.setSelection(selection);
		}
	}
	
	public String getType(){
		return tx_Type.getText();
	}

	public void setType(String newType) {
		if( newType != null && ! tx_Type.getText().equals(newType)){
			Point selection = tx_Type.getSelection();
			tx_Type.setText(newType);
			tx_Type.setSelection(selection);
		}
	}

	public String getValueKreuzer(){
		return tx_ValueKreuzer.getText();
	}

	public void setValueKreuzer(String newValueKreuzer) {
		if( newValueKreuzer != null && ! tx_ValueKreuzer.getText().equals(newValueKreuzer)){
			Point selection = tx_ValueKreuzer.getSelection();
			tx_ValueKreuzer.setText(newValueKreuzer);
			tx_ValueKreuzer.setSelection(selection);
		}
	}
	
	public String getQualityMin(){
		return tx_QualityMin.getText();
	}

	public void setQualityMin(String newQualityMin) {
		if( newQualityMin != null && ! tx_QualityMin.getText().equals(newQualityMin)){
			Point selection = tx_QualityMin.getSelection();
			tx_QualityMin.setText(newQualityMin);
			tx_QualityMin.setSelection(selection);
		}
	}
	
	public String getQualityMax(){
		return tx_QualityMax.getText();
	}

	public void setQualityMax(String newQualityMax) {
		if( newQualityMax != null && ! tx_QualityMax.getText().equals(newQualityMax)){
			Point selection = tx_QualityMax.getSelection();
			tx_QualityMax.setText(newQualityMax);
			tx_QualityMax.setSelection(selection);
		}
	}
	
	public CompositeEditShapenessDescriptions getCompositeBrokenness() {
		return cp_Brokenness;
	}

	public CompositeEditShapenessDescriptions getCompositeDirtiness() {
		return cp_Dirtiness;
	}


	public void setDecorationWeightStatus(boolean active){
		if(active){
			decoration_weight.show();
		}else{
			decoration_weight.hide();
		}
	}
	public void setDecorationQualityMinStatus(boolean active){
		if(active){
			decoration_QualityMin.show();
		}else{
			decoration_QualityMin.hide();
		}
	}
	public void setDecorationQualityMaxStatus(boolean active){
		if(active){
			decoration_QualityMax.show();
		}else{
			decoration_QualityMax.hide();
		}
	}
	public void setDecorationValueKreuzerStatus(boolean active){
		if(active){
			decoration_ValueKreuzer.show();
		}else{
			decoration_ValueKreuzer.hide();
		}
	}

	
}
